<?php

/**
 * 用户相关
 */

namespace app\api\controller;


use app\api\model\BusinessArea;
use app\api\model\IntervalConfig;
use app\api\model\Region;
use app\api\model\UserAttention;
use app\api\model\UserRemind;
use app\common\controller\ApiController;
use app\common\lib\RedisKey;
use app\common\lib\RespCode;
use custom\Token;
use Login\WangYiSms3;
use think\Db;
use think\Exception;
use app\api\model\User as UserModel;
use app\api\model\HouseVillage as HouseVillageModel;

class User extends ApiController {

//    protected $notNeedToken = ['demandbefore', 'getvillage', 'getarea'];

    protected $notNeedPhone = ['demandbefore', 'getvillage', 'getarea'];

    protected $isArrayResult = ['getvillage', 'getarea', 'showcollect'];

    protected $resubmitLock = ['editnickname', 'editavatar'];

    /**
     * ver 1.0.1
     * 购房需求提供的区域数据
     */
    public function demandBefore() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $Region = new Region();
            $area_list = $Region->get_area_list($params);//区域
            $price_arr = IntervalConfig::all_intervel($params['city_code'])['price_interval'];//预算
            $area_arr = IntervalConfig::all_intervel($params['city_code'])['build_area_interval'];//理想面积
            $door_arr = IntervalConfig::all_intervel($params['city_code'])['house_door'];//户型
            $city_code = $params['city_code'];
            return $this->resData('success', compact('area_list', 'price_arr', 'area_arr','door_arr', 'city_code'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 增加 不限选项
     * @return false|string
     */
    public function demandBeforeUnlimited()
    {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $Region = new Region();
            $area_list = $Region->get_area_list($params);//区域
            $all_area=['name'=>'不限','city_code'=>$params['city_code'].'_all'];
            array_unshift($area_list, $all_area);
            $price_arr = IntervalConfig::all_intervel($params['city_code'])['price_interval'];//预算
            $area_arr = IntervalConfig::all_intervel($params['city_code'])['build_area_interval'];//理想面积
            $door_arr = IntervalConfig::all_intervel($params['city_code'])['house_door'];//户型
            $city_code = $params['city_code'];
            return $this->resData('success', compact('area_list', 'price_arr', 'area_arr','door_arr', 'city_code'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取小区
     */
    public function getVillage() {
        try {
            $params = $this->checkParams('city_code');
            if ($params === false) {
                return $this->resErr();
            }
            $data = HouseVillageModel::getV($params);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取商圈
     */
    public function getArea() {
        try {
            $params = $this->checkParams('city_code');
            if ($params === false) {
                return $this->resErr();
            }
            $data = BusinessArea::getA($params);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加关注或者提醒
     * @param id 收藏id
     * @param c_type 1:关注，2:提醒
     * @param r_type 1：房源，2：小区
     * @return false|string
     */
    public function addCollect() {
        try {
            $params = $this->checkParams('id,c_type,r_type');
            if ($params === false) {
                return $this->resErr();
            }
            //关注
            if ($params['c_type'] == 1) {
                $re = UserAttention::add($params);
                if ($re) {
                    return $this->resData('操作成功', []);
                }
            }
            //提醒
            if ($params['c_type'] == 2) {
                $re = UserRemind::add($params);
                if ($re) {
                    return $this->resData('操作成功', []);
                }
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除关注或者提醒
     * @param ids 收藏id
     * @param c_type 1:关注，2:提醒
     * @return false|string
     */
    public function delCollect() {
        try {
            $params = $this->checkParams('ids,c_type,r_type');
            if ($params === false) {
                return $this->resErr();
            }
            //关注
            if ($params['c_type'] == 1) {
                UserAttention::del($params);
                return $this->resData('操作成功', []);
            }
            //提醒
            if ($params['c_type'] == 2) {
                UserRemind::del($params);
                return $this->resData('操作成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 显示关注或者提醒房源
     * @param c_type 1:关注，2:提醒
     * @return false|string
     */
    public function showCollect() {
        try {
            $params = $this->checkParams('c_type,r_type');
            if ($params === false) {
                return $this->resErr();
            }
            //关注
            if ($params['c_type'] == 1) {
                $re = UserAttention::show($params);
            }
            //提醒
            if ($params['c_type'] == 2) {
                $re = UserRemind::show($params);
            }
            return $this->resData('success', $re);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 用户个人信息
     * @return false|string
     */
    public function personalHomePage() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $re = UserModel::getpersonal($params);
            return $this->resData('success', $re);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改昵称
     * @return false|string
     */
    public function editNickname() {
        try {
            $params = $this->checkParams('nickname');
            if ($params === false) {
                return $this->resErr();
            }
            $re = UserModel::editNickname($params);
            if (!$re) {
                throw new Exception('请输入新昵称', RespCode::DATA_ERR);
            } else {
                return $this->resData('操作成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 验证原手机号
     * @return false|string
     */
    public function checkOldMobile() {
        try {
            $params = $this->checkParams('code');
            if ($params === false) {
                return $this->resErr();
            }
            $user = Db::name("user")->field('phone')->find($params['user_id']);
            $re = WangYiSms3::verifySms($user['phone'], $params['code'], RedisKey::OLD_MOBILE_CODE);
            if ($re) {//验证码通过
                $Token = new Token();
                $token_ = $Token->getMobileToken($params['user_id'] . '-' . $user['phone']);
                return $this->resData('success', ['edit_auth' => $token_]);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 验证新手机
     * @return false|string
     */
    public function checkNewMobile() {
        try {
            $params = $this->checkParams('code,new_phone,edit_auth');
            if ($params === false) {
                return $this->resErr();
            }
            $re = WangYiSms3::verifySms($params['new_phone'], $params['code'], RedisKey::NEW_MOBILE_CODE);
            if ($re) {//验证码通过
                $Token = new Token();
                $info = $Token->verifyMobileToken($params['edit_auth']);
                $user = Db::name("user")->field('phone')->find($params['user_id']);
                if ($params['user_id'] . '-' . $user['phone'] != $info) {
                    throw new Exception('令牌错误，请重新验证原手机', RespCode::DATA_ERR);
                } else {
                    $res = UserModel::editMobile($params['new_phone'], $params['user_id']);
                    if (!$res) {
                        throw new Exception('该手机已被绑定', RespCode::QUERY_ERR);
                    } else {
                        return $this->resData('操作成功', []);
                    }
                }
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 注销
     */
    public function Logout() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            UserModel::del_user($params, $this->headers);
            return $this->resData('操作成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改头像
     */
    public function editAvatar() {
        try {
            $params = $this->checkParams('avatar');
            if ($params === false) {
                return $this->resErr();
            }
            UserModel::editAvatar($params);
            $this->resData('操作成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 定制选房->小区列表数据
     * @return false|string
     */
    public function getVillageList() {
        try {
            $params = $this->checkParams('page,city_code,area_code');
            if ($params === false) {
                return $this->resErr();
            }
            $HouseVillageModel=new HouseVillageModel();
            $pageSize=20;//每页显示条数
            $data =$HouseVillageModel->getVillageList($params,$pageSize);
            if (!$data){
                return $this->resErr();
            }else{
                return $this->resData('success', $data);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 定制选房->商圈列表数据
     * @return false|string
     */
    public function getBusinessList() {
        try {
            $params = $this->checkParams('page,city_code,area_code');
            if ($params === false) {
                return $this->resErr();
            }
            $BusinessAreaModel=new BusinessArea();
            $pageSize=20;//每页显示条数
            $data =$BusinessAreaModel->getBusinessList($params,$pageSize);
            if (!$data){
                return $this->resErr();
            }else{
                return $this->resData('success', $data);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }
}
